package com.ruoyi.generator.controller.ysd;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.domain.GenForm;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableConfig;
import com.ruoyi.generator.service.IFromCrudBtnsService;
import com.ruoyi.generator.service.IFromCrudTableConfigService;
import com.ruoyi.generator.service.IGenTableService;
import com.ruoyi.generator.service.impl.FromBuildConfigServiceImpl;
import com.ruoyi.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CRUD 表单
 */
@RestController
public class YsdCurdFormController extends BaseController
{
    @Autowired
    private IGenTableService genTableService;

    @Autowired
    private FromBuildConfigServiceImpl fromBuildConfigService;

    @Autowired
    private IFromCrudTableConfigService fromCrudTableConfigService;

    /**
     * 保存表单设计信息
     * @return
     */
    @PostMapping("/form/save")
    public AjaxResult genFormSave(@RequestBody List<GenForm> genForms) throws Exception {

        if(genForms==null){
            return AjaxResult.error("表单设计信息不能为空");
        }
        AjaxResult result = AjaxResult.success();
        for (GenForm genForm:genForms){
            if(StringUtils.isEmptyIncludeNull(genForm.getClientType())){
                genForm.setClientType("PC");
            }
            /**
             * 保存表单设计信息
             */
            GenTable genTable =new GenTable();
            genTable.setTableId(genForm.getTableId());
            genTable.setTableName(genForm.getTableName());
            genTable.setTableType(genForm.getTableType());
            result = fromBuildConfigService.saveFormInfo(genTable, genForm.getFormBuildKey(), genForm.getClientType(), genForm.getFromBuildConfig());
            if(result.isError()){
                return result;
            }
            /** 业务数据风格配置对象 **/
            fromCrudTableConfigService.refreshTableConfig(genForm.getFormBuildKey(),null);
        }
        return result;
    }

    /**
     * 创建对应数据库表，并生成对应基础的增、删、改、查功能
     * @return
     */
    @PostMapping("/page/gener/crud")
    public AjaxResult generCrudTable(@RequestBody GenForm genForm) throws Exception {
        return genTableService.generCrudTable(genForm);
    }

    /**
     * 保存表单设计信息
     * @return
     */
    @PostMapping("/page/gener/build/save")
    public AjaxResult savePageGenerBuild(@RequestBody GenForm genForm) throws Exception {
        return genTableService.savePageGenerBuild(genForm);
    }

    /**
     * 保存模块基本配置信息
     * @return
     */
    @PostMapping("/page/gener/table/config/save")
    public AjaxResult savePageGenerBuild(@RequestBody GenTableConfig genTableConfig) throws Exception {
        return genTableService.saveTableGenerConfig(genTableConfig);
    }

}
